(function(app, Novel) {
    var baseUrl = 'http://m.baidu.com/tc';
    var novelListModel = app.models.novelList;
    var chapterGroup = novelListModel.get('group');

    function getChapter(index, callback) {
        var paramsTemplate = {
            srd: 1,
            appui: 'alaxs',
            ajax: 1,
            gid: '1768429347',
            pageType: undefined,
            src: 'http://www.qingxinwang.com/files/article/html/16/16830/2083470.html',
            cid: '1768429347|4715056921788593870',
            time: '',
            skey: '',
            id: 'wisenovel'
        };
	params = {};
        for (var key in paramsTemplate) {
            params[key] = novelListModel.get(key);
        }
        params['ajax'] = 1;
        params['pageType'] = undefined;
        params['src'] = chapterGroup[index].href;
        params['cid'] = chapterGroup[index].cid;
        $.extend(params, Novel.verify);
        var defer = $.ajax({
            url: baseUrl,
            dataType: 'json',
            data: params,
            success: callback
        });

        //        return defer;
    }

    var chapterContents = [];

    function getAllChapter() {
        var getByIndexQueue = function(idx) {
            getChapter(idx, (function(index) {
                return function(data) {
                    chapterContents.push(data);
                    if (index < chapterGroup.length - 1) {
                        getByIndexQueue(index + 1);
                    }
                }
            })(idx));
        }

        getByIndexQueue(0);
    }

    function checkAllDone() {
        return chapterContents && chapterContents.length === chapterGroup.length;
    }

    var exports = {
        checkAllDone: checkAllDone,
        getAllChapter: getAllChapter,
        getChapter: getChapter,
	chapterContents:chapterContents,
	chapterGroup:chapterGroup
    };


    app.injectExtension = exports;
})(app, Novel);
